﻿using Aierp.SystemEntities.System;
using Aierp.SystemRepository.Models;
using Aierp.Utility.Data;

namespace Aierp.SystemRepository.Implements
{
    /// <summary>
    /// 词典分类
    /// </summary>
    public class DicsRepository : DbInstance
    {
        /// <summary>
        /// 查询一个名称--根据名称和编码
        /// </summary>
        /// <param name="title"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        public Sys_Dics Find(string title, string code)
        {
            var result = this.FindBy<Sys_Dics>(c => c.Title == title);
            if (result != null) return result;

            result = this.FindBy<Sys_Dics>(c => c.Code == code);
            return result;
        }

        /// <summary>
        /// 同时查询名称和编码
        /// </summary>
        /// <param name="title"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        public Sys_Dics FindByTitleAndCode(string title, string code)
        {
            return this.FindBy<Sys_Dics>(c => c.Title == title && c.Code == code);
        }

        /// <summary>
        /// 查询一个编码-根据ID，只返回指定信息，密码等关键信息不返回
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public Sys_Dics FindById(int key)
        {
            return this.FindBy<Sys_Dics>(c => c.KeyId == key);
        }

        /// <summary>
        /// 查询一个编码-根据ID，只返回指定信息，密码等关键信息不返回
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public Sys_Dics FindByCategoryId(int key)
        {
            var status = EnumStatus.POST.ToString();
            return this.FindBy<Sys_Dics>(c => c.CategoryId == key && c.Status == status);
        }

        /// <summary>
        /// 获取一个列表，供给Grid使用
        /// </summary>
        /// <param name="sortOrder"></param>
        /// <param name="limit"></param>
        /// <param name="offset"></param>
        /// <param name="searchTxt"></param>
        /// <returns></returns>
        public string GetList(string sortOrder, int limit, int offset, string searchTxt)
        {
            if (!string.IsNullOrEmpty(searchTxt))
            {
                int.TryParse(searchTxt, out var keyId);
                var status = EnumStatus.POST.ToString();

                return this.GetPageList<Sys_Dics, object>(limit, offset, c => c.CategoryId == keyId && c.Status == status, c => c.Sortnum, sortOrder);
            }
            return this.GetPageList<Sys_Dics, object>(limit, offset, null, c => c.Sortnum, sortOrder);

        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public int Add(Sys_Dics item)
        {
            if (item == null) return 0;

            var model = this.Find(item.Title, item.Code);
            if (model != null) return -2;

            //如果传入默认值为true，则统一更新该类其他的默认值为false
            if (item.IsDefault) SetDefault(item.CategoryId);

            item.Status = EnumStatus.POST.ToString();
            item.IsAllowModify = true;
            item.IsAllowDelete = true;
            var ret = Db.Insert(item);

            return ret?.KeyId ?? 0;
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public int Update(Sys_Dics item)
        {
            //先检查是否存在
            var model = this.FindById(item.KeyId);
            if (model == null) return -2;

            //检查名称和编码是否同时存在
            model = this.Find(item.Title, item.Code);
            if (model != null) return -9;

            //如果不允许修改，则返回消息
            if (!model.IsAllowModify) return -3;

            //如果传入默认值为true，而且数据库中的值为false,则代表修改过，则统一更新其他的默认值为false
            if(item.IsDefault && model.IsDefault==false) SetDefault(item.CategoryId);

            //更新
            Db.TrackEntity(model);//在上下文中跟踪实体
            model.Title = item.Title;
            model.Code = item.Code;
            model.Sortnum = item.Sortnum;
            model.IsDefault = item.IsDefault;
            return Db.Update(model);//这时只会更新被修改的字段
        }

        /// <summary>
        /// 删除-不做物理删除，仅将状态字段修改为DELETE
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public int Remove(int key)
        {
            //先检查是否存在
            var model = this.FindById(key);
            if (model == null) return -2;

            //如果不允许删除，则返回消息
            if (!model.IsAllowDelete) return -3;

            //更新
            Db.TrackEntity(model);//在上下文中跟踪实体
            model.Status = EnumStatus.DELETE.ToString();
            return Db.Update(model);//这时只会更新被修改的字段
        }

        /// <summary>
        /// 设置默认值
        /// </summary>
        /// <param name="categoryId"></param>
        public void SetDefault(int categoryId)
        {
            Db.Update<Sys_Dics>(a => a.CategoryId == categoryId, a => new Sys_Dics() { IsDefault = false });
        }
    }
}
